package com.lightbend.akka.ddata

import akka.actor.ActorSystem
import akka.cluster.sharding._
import com.lightbend.akka.ddata.CalculatorShard.CalcCommands
import com.typesafe.config.ConfigFactory

object ClusterShardingDemo extends App {

  CalcShards.create(2551)
  CalcShards.create(0)
  CalcShards.create(0)
  CalcShards.create(0)


  val shardingSystem = ActorSystem("ShardingSystem", ConfigFactory.load("sharding"))
  CalcShards.startupSharding(0, shardingSystem)


  val calcRegion = ClusterSharding(shardingSystem).shardRegion(CalculatorShard.shardName)

  calcRegion ! CalcCommands("1012", Calculator.Num(13.0)) //shard 1, entity 1012
  calcRegion ! CalcCommands("1012", Calculator.Add(12.0))
  calcRegion ! CalcCommands("1012", Calculator.ShowResult) //shows address too
  calcRegion ! CalcCommands("1012", Calculator.Disconnect) //disengage cluster

  calcRegion ! CalcCommands("2012", Calculator.Num(10.0)) //shard 2, entity 2012
  calcRegion ! CalcCommands("2012", Calculator.Mul(3.0))
  calcRegion ! CalcCommands("2012", Calculator.Div(2.0))
  calcRegion ! CalcCommands("2012", Calculator.Div(0.0)) //divide by zero

  calcRegion ! CalcCommands("1012", Calculator.ShowResult) //check if restore result on another node
  calcRegion ! CalcCommands("2012", Calculator.ShowResult)
}